import request from '@/utils/request'

type Common<T> = {
  code: string
  data: T
  mesg: string
  time: string
}

// 按条件查询角色信息
export type TCondition = Partial<{
  code: string
  id: number
  name: string
  startCreateTime: string
  endCreateTime: string
  current: number
  size: number
}>

export type TQueryResult = Partial<{
  current: number
  hitCount: boolean
  optimizeCountSql: boolean
  orders: any[]
  pages: number
  records: TRole[] | []
  searchCount: boolean
  size: number
  total: number
}>

export type TRole = {
  code: string
  createdBy: string
  createdTime: string
  description: string
  id: number
  name: string
  operatorId: number | null
  updateBy: string
  updatedTime: string
}

export const getRolePages = (condition: TCondition) => {
  return request<Common<TQueryResult>>({
    method: 'POST',
    url: '/boss/role/getRolePages',
    data: condition
  })
}

// 获取角色菜单信息
export type TMenuItem = {
  createdBy: string
  createdTime: string
  description: string
  href: string
  icon: string
  id: number
  level: number
  name: string
  operatorId: number | null
  orderNum: number
  parentId: number
  selected: boolean
  shown: boolean
  subMenuList: TMenuItem[] | []
  updatedBy: string
  updatedTime: string
}
export const getRoleMenus = (roleId: string) => {
  return request<Common<TMenuItem[]>>({
    method: 'GET',
    url: '/boss/menu/getRoleMenus',
    params: { roleId }
  })
}
// 给角色分配菜单信息
export const allocateRoleMenus = (roleId: string, menuIdList: number[]) => {
  return request<Common<boolean>>({
    method: 'POST',
    url: '/boss/menu/allocateRoleMenus',
    data: {
      roleId,
      menuIdList
    }
  })
}

// 获取角色拥有的资源列表
export type TCategoryItem = {
  createdBy: string
  createdTime: string
  id: number
  name: string
  operatorId: number | null
  resourceList: [] | TResourceItem[]
  selected: boolean
  sort: number
  updatedBy: string
  updatedTime: string
}
export type TResourceItem = {
  categoryId: number
  createdBy: string
  createdTime: string
  description: string
  id: number
  name: string
  operatorId: number | null
  selected: boolean
  updatedBy: string
  updatedTime: string
  url: string
}

export const getRoleResources = (roleId: string) => {
  return request<Common<TCategoryItem[]>>({
    method: 'GET',
    url: '/boss/resource/getRoleResources',
    params: { roleId }
  })
}

// 给角色重新分配资源
export const allocateRoleResources = (roleId: string, resourceList: number[]) => {
  return request<Common<boolean>>({
    method: 'POST',
    url: '/boss/resource/allocateRoleResources',
    data: {
      roleId,
      resourceList
    }
  })
}
